#include "serialform.h"
#include "ui_serialform.h"
#include <QDebug>
#include <QMessageBox>
#include <QRegularExpression>
#include <QTextCursor>
#include <QScrollBar>


SerialForm::SerialForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SerialForm)
{
    ui->setupUi(this);
    setLayout(ui->Serial_Layout);

}

SerialForm::~SerialForm()
{
    delete ui;
}


/*  函数名称: on_ClearRev_Btn_released
 *  函数作用: 清除接收回调函数
 *
 */
void SerialForm::on_ClearRev_Btn_released()
{
    ui->SerialShow_Edit->clear();
}

/*  函数名称: on_ClearSend_Btn_released
 *  函数作用: 清除发送回调函数
 *
 */
void SerialForm::on_ClearSend_Btn_released()
{
    ui->SerialSend_Edit->clear();
}

/*  函数名称: on_SerialSend_Edit_textChanged
 *  函数作用: 发送框输入回调函数
 *
 */
void SerialForm::on_SerialSend_Edit_textChanged()
{
    if(ui->HaxSend_Box->isChecked()) //16进制发送
    {
        QString Str = ui->SerialSend_Edit->toPlainText();
        if(Str.isEmpty())return;
        Str.remove(QRegularExpression("\\s")); // 移除所有空格
        QRegularExpression regex("^[A-Fa-f0-9]+$");
        QRegularExpressionMatch match = regex.match(Str);
        if(match.hasMatch())
        {
            LastSend = ui->SerialSend_Edit->toPlainText();
        }
        else
        {
            ui->SerialSend_Edit->setPlainText(LastSend);

            QTextCursor cursor = ui->SerialSend_Edit->textCursor();
            cursor.movePosition(QTextCursor::End);
            ui->SerialSend_Edit->setTextCursor(cursor);

            QMessageBox msgBox;
            msgBox.setText(tr("请输入正确格式 0~9 或者 A~F 或者 a~f 或者空格"));
            msgBox.exec();
        }
    }
}

/*  函数名称: on_HaxSend_Box_clicked
 *  函数作用: 发送框16进制转换
 *
 */
void SerialForm::on_HaxSend_Box_clicked()
{
    QString Str = ui->SerialSend_Edit->toPlainText();
    if(ui->HaxSend_Box->isChecked())
    {
        if(!Str.isEmpty())
        {
            QString Tstr = Str.toUtf8().toHex();
            LastSend.clear();
            for (int i = 0; i < Tstr.length(); i += 2)
            {
                LastSend += Tstr.mid(i, 2) + " ";
            }
            ui->SerialSend_Edit->setPlainText(LastSend);
        }
        else
        {
          LastSend.clear();
        }
    }
    else
    {
        if(!Str.isEmpty())
        {
            Str.remove(' '); // 移除空格
            QByteArray byteArray = QByteArray::fromHex(Str.toUtf8());
            ui->SerialSend_Edit->setPlainText(QString::fromUtf8(byteArray));
        }
    }
}

/*  函数名称: RevSerialDat
 *  函数作用: 串口接收数据
 *
 */
void SerialForm::RevSerialDat(QString Str)
{
    QString Dat = ui->SerialShow_Edit->toPlainText();
    if(ui->HaxRev_Box->isChecked())
    {

        for (int i = 0; i < Str.length(); i += 2)
        {
            Dat += Str.mid(i, 2) + " ";
        }
        ui->SerialShow_Edit->setPlainText(Dat);
    }
    else
    {
        Dat = Dat + QString::fromUtf8(QByteArray::fromHex(Str.toUtf8()));
        ui->SerialShow_Edit->setPlainText(Dat);
    }

    ui->SerialShow_Edit->moveCursor(QTextCursor::End);
    ui->SerialShow_Edit->ensureCursorVisible();
}

/*  函数名称: on_Send_Btn_released
 *  函数作用: 串口发送数据
 *
 */
void SerialForm::on_Send_Btn_released()
{
    QString Str = ui->SerialSend_Edit->toPlainText();

    if(ui->HaxSend_Box->isChecked())
    {
       Str.remove(' ');
       Str = QString::fromUtf8(QByteArray::fromHex(Str.toUtf8()));
    }
    if(ui->NewLine_Box->isChecked())
    {
        Str+= "\r\n";
    }
    emit SendSerialDat(Str);
}
